package gold.digger;

import gold.utils.InputUtil;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC978 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int maxTurbulenceSize(int[] nums) {
            if (nums.length <= 1) return nums.length;
            if (nums.length == 2) return nums[0] == nums[1] ? 1 : 2;
            int curLen = nums[1] == nums[0] ? 1 : 2, maxLen = 0, curCompareBefore = 0;
            for (int i = 1; i < nums.length - 1; i++) {
                if ((nums[i - 1] < nums[i] && nums[i] > nums[i + 1]) || (nums[i - 1] > nums[i] && nums[i] < nums[i + 1])) {
                    ++curLen;
                } else {
                    maxLen = Math.max(curLen, maxLen);
                    curLen = nums[i - 1] == nums[i] && nums[i + 1] == nums[i] ? 1 : 2;
                }
            }

            return Math.max(curLen, maxLen);
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.maxTurbulenceSize(InputUtil.toIntegerArray("[9,4,2,10,7,8,8,1,9]")));
        System.out.println(solution.maxTurbulenceSize(InputUtil.toIntegerArray("[4,8,12,16]")));
        System.out.println(solution.maxTurbulenceSize(InputUtil.toIntegerArray("[0,8,45,88,48,68,28,55,17,24]")));
        System.out.println(solution.maxTurbulenceSize(InputUtil.toIntegerArray("[10,10,10]")));
        System.out.println(solution.maxTurbulenceSize(InputUtil.toIntegerArray("[0,1,1,0,1,0,1,1,0,0]")));
        System.out.println(solution.maxTurbulenceSize(InputUtil.toIntegerArray("[37,199,60,296,257,248,115,31,273,176]")));
    }

    public static void main(String[] args) throws Exception {
        LC978 an = new LC978();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
